﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
namespace Rabbit.Utils
{
    public static  class ExtensionMethod
    {
        public static int Find<T>(this ObservableCollection<T> Collection, Predicate<T> match)
        {
            for (int i = 0; i < Collection.Count; i++)
            {
                if (match(Collection[i]))
                {
                    return i;
                }
            }
            return -1;
        }
        public static void Remove<T>(this ObservableCollection<T> Collection, Predicate<T> match)
        {
            int destIndex = -1;
            for (int i = 0; i < Collection.Count; i++)
            {
                if (match(Collection[i]))
                {
                    destIndex = i;
                }
            }
            if (destIndex != -1)
            {
                Collection.RemoveAt(destIndex);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Collection"></param>
        /// <param name="OtherList"></param>
        /// <param name="MaxCollectionCount"></param>
        /// <param name="ListStartIndex"></param>
        public static void InsertRange<T>(this ObservableCollection<T> Collection, List<T> OtherList,int MaxCollectionCount)
        {
            int EndIndex = OtherList.Count;

            if (OtherList.Count > MaxCollectionCount)
            {
                EndIndex = MaxCollectionCount;
            }

            int j = 0;
            for (int i = 0; i < EndIndex; i++)
            {
                Collection.Insert(j, OtherList[i]);

                j++;
                if (Collection.Count > MaxCollectionCount)
                {
                    Collection.RemoveAt(Collection.Count - 1);
                }
            }
        }

        public static void ForEach<T>(this IEnumerable<T> Collection, Action<T> action)
        {
            foreach (var item in Collection)
            {
                action(item);
            }
        }
    }
}
